import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:tencent_cloud_chat_uikit/base_widgets/tim_ui_kit_statelesswidget.dart';
import 'package:tencent_cloud_chat_uikit/business_logic/separate_models/tui_chat_separate_view_model.dart';
import 'package:tencent_cloud_chat_uikit/business_logic/view_models/tui_chat_global_model.dart';
import 'package:tencent_cloud_chat_uikit/czm_custom_widget/czm_widget_bridge.dart';

import 'package:tencent_im_base/tencent_im_base.dart';

import 'package:tencent_cloud_chat_uikit/base_widgets/tim_ui_kit_base.dart';

class MultiSelectPanel extends TIMUIKitStatelessWidget {
  final ConvType conversationType;

  MultiSelectPanel({Key? key, required this.conversationType}) : super(key: key);

  _handleForwardMessage(BuildContext context, bool isMergerForward, TUIChatSeparateViewModel model) {
    CZMWidgetBuilderBridge.getInstance().toForwardMessagePage(conversationType, isMergerForward, model);
  }

  @override
  Widget tuiBuild(BuildContext context, TUIKitBuildValue value) {
    final TUIChatSeparateViewModel model = Provider.of<TUIChatSeparateViewModel>(context);
    final safeBottom = MediaQuery.of(context).padding.bottom;
    return SafeArea(
      top: false,
      child: Container(
        color: hexToColor("f5f5f6"),
        padding: EdgeInsets.only(top: 40, left: 62.5, right: 62.5, bottom: safeBottom > 0 ? 20 : 40),
        child: Row(
          children: [
            _Item(
              label: '逐条转发',
              iconPath: 'images/message_forward.webp',
              onTap: () => _handleForwardMessage(context, false, model),
              enbale: model.multiSelectedMessageList.isNotEmpty && model.canForwordMessage,
            ),
            const Spacer(),
            _Item(
              label: '合并转发',
              iconPath: 'images/message_forward_all.webp',
              onTap: () => _handleForwardMessage(context, true, model),
              enbale: model.multiSelectedMessageList.isNotEmpty && model.canForwordMessage,
            ),
            const Spacer(),
            _Item(
              label: '删除',
              iconPath: 'images/message_delete.webp',
              onTap: () => _delete(context, model),
              enbale: model.multiSelectedMessageList.isNotEmpty,
            ),
          ],
        ),
      ),
    );
  }

  Future<void> _delete(BuildContext context, TUIChatSeparateViewModel model) async {
    final confirm = await CZMWidgetBuilderBridge.getInstance().msDialog(
      context,
      desc: "确定删除该条消息吗？",
      content: '删除后将无法恢复，请谨慎操作',
    );
    if (confirm == true) {
      model.deleteSelectedMsg();
      model.updateMultiSelectStatus(false);
    }
  }
}

class _Item extends StatelessWidget {
  final String label;
  final String iconPath;
  final VoidCallback? onTap;
  final bool enbale;

  const _Item({Key? key, required this.label, required this.iconPath, this.onTap, this.enbale = true}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    Widget child = GestureDetector(
      onTap: onTap,
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Container(
            width: 50,
            height: 50,
            alignment: Alignment.center,
            decoration: BoxDecoration(borderRadius: BorderRadius.circular(25), color: Colors.white),
            child: Image.asset(iconPath, package: 'tencent_cloud_chat_uikit', width: 28, height: 28),
          ),
          const SizedBox(height: 8),
          Text(TIM_t(label),
              style: const TextStyle(color: Color(0xFF1D2541), fontSize: 12, fontWeight: FontWeight.normal))
        ],
      ),
    );

    if (!enbale) {
      child = IgnorePointer(
        child: Opacity(
          opacity: 0.5,
          child: child,
        ),
      );
    }

    return child;
  }
}
